package com.robert.socketiodemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private Socket mSocket;
    private String url = "https://pusherio.9002.sayahao.com?app_id657cbdb?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6IjRmMWcyM2ExMmFhIn0.eyJpc3MiOiJodHRwOlwvXC9leGFtcGxlLmNvbSIsImF1ZCI6Imh0dHA6XC9cL2V4YW1wbGUub3JnIiwianRpIjoiNGYxZzIzYTEyYWEiLCJpYXQiOjE1MTE5NDI4NzYsIm5iZiI6MTUxMTk0MjkzNiwiZXhwIjoxNTEyMDI5Mjc2LCJ1aWQiOiIxMjgiLCJ0eXBlIjoidXNlciJ9.3bZA3Q1dHZKNV5f8663q5SImVcKYdQL0no9tug_y6DQ";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            mSocket = IO.socket(url);
            mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

                @Override
                public void call(Object... args) {
                    Log.e(TAG, "EVENT_CONNECT: ");
                    for (Object arg : args) {
                        Log.e(TAG, "server response: " + arg);
                    }
//                    mSocket.disconnect();
                }

            }).on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    Log.e(TAG, "EVENT_MESSAGE: ");
                    for (Object arg : args) {
                        Log.e(TAG, "disconnect: " + arg);
                    }
                }
            }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {

                @Override
                public void call(Object... args) {
                    Log.e(TAG, "EVENT_DISCONNECT: ");
                    for (Object arg : args) {
                        Log.e(TAG, "disconnect: " + arg);
                    }
                }
            }).on(Socket.EVENT_ERROR, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    Log.e(TAG, "EVENT_ERROR: ");
                    for (Object arg : args) {
                        Log.e(TAG, "disconnect: " + arg);
                    }
                }
            });
            mSocket.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
